home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / FWPrInfo.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  36.2 KB  |  1,437 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPrInfo.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPRINFO_H
  13. #include "FWPrInfo.h"
  14. #endif
  15.  
  16. #ifndef FWMEMORY_H
  17. #include "FWMemory.h"
  18. #endif
  19.  
  20. #ifndef FWBARRAY_H
  21. #include "FWBArray.h"
  22. #endif
  23.  
  24. #ifndef FWACQUIR_H
  25. #include "FWAcquir.h"
  26. #endif
  27.  
  28. #ifndef FWGXUTIL_H
  29. #include "FWGXUtil.h"
  30. #endif
  31.  
  32. #ifndef FWGRUTIL_H
  33. #include "FWGrUtil.h"
  34. #endif
  35.  
  36. #ifndef FWPRHDLR_H
  37. #include "FWPrHdlr.h"
  38. #endif
  39.  
  40. #ifndef FWSTRS_H
  41. #include "FWStrs.h"
  42. #endif
  43.  
  44. #ifndef FWSESION_H
  45. #include "FWSesion.h"
  46. #endif
  47.  
  48. // ----- QuickDraw GX includes
  49.  
  50. #if defined(FW_SUPPORT_GX) && !defined(__GXGRAPHICS__)
  51. #include <GXGraphics.h>
  52. #endif
  53.  
  54. #if defined(FW_SUPPORT_GX) && !defined(__GXENVIRONMENT__)
  55. #include <GXEnvironment.h>
  56. #endif
  57.  
  58. // ----- OpenDoc includes
  59.  
  60. #ifndef SOM_ODSession_xh
  61. #include <ODSessn.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODArbitrator_xh
  65. #include <Arbitrat.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODFoci_xh
  69. #include <Foci.xh>
  70. #endif
  71.  
  72. #ifndef SOM_ODFrame_xh
  73. #include <Frame.xh>
  74. #endif
  75.  
  76. #ifndef SOM_ODWindowState_xh
  77. #include <WinStat.xh>
  78. #endif
  79.  
  80. #ifndef SOM_ODStorageUnit_xh
  81. #include <StorageU.xh>
  82. #endif
  83.  
  84. #ifndef SOM_ODCanvas_xh
  85. #include <Canvas.xh>
  86. #endif
  87.  
  88. #ifndef SOM_ODFacet_xh
  89. #include <Facet.xh>
  90. #endif
  91.  
  92. //========================================================================================
  93. // RunTime Info
  94. //========================================================================================
  95.  
  96. #ifdef FW_BUILD_MAC
  97. #pragma segment odfprinting
  98. #endif
  99.  
  100. FW_DEFINE_AUTO(FW_CPrintEnvironment)
  101. FW_DEFINE_AUTO(FW_CPrintInfo)
  102. FW_DEFINE_AUTO(FW_CPrintJob)
  103.  
  104. //========================================================================================
  105. // Local helpers
  106. //========================================================================================
  107.  
  108. static void    SaveHandle(
  109.             Environment*         ev,
  110.             ODStorageUnit*         storageUnit,
  111.             const char*            propName,
  112.             const char*            propType,
  113.             FW_PlatformHandle    handle);
  114.             
  115. static void    SaveBuffer(
  116.             Environment*         ev,
  117.             ODStorageUnit*         storageUnit,
  118.             const char*            propName,
  119.             const char*            propType,
  120.             const void*            buffer,
  121.             unsigned long        bufSize);
  122.             
  123. static FW_Boolean LoadHandle(
  124.             Environment*         ev,
  125.             ODStorageUnit*         storageUnit,
  126.             const char*            propName,
  127.             const char*            propType,
  128.             FW_PlatformHandle&    handle);
  129.  
  130. static FW_Boolean LoadBuffer(
  131.             Environment*         ev,
  132.             ODStorageUnit*         storageUnit,
  133.             const char*            propName,
  134.             const char*            propType,
  135.             void*                buffer,
  136.             unsigned long&        bufSize);
  137.  
  138. #if defined(FW_SUPPORT_GX) && defined(FW_BUILD_MAC)
  139.  
  140. static OSErr    MacGXSpoolProc(gxShape currentShape, long refCon);
  141.  
  142. struct SMacGXSpool
  143. {
  144.     gxViewPort        fGXViewPort;
  145.     gxJob            fGXPrintJob;
  146.     FW_CRect        fPageBounds;
  147. };
  148.  
  149. static SMacGXSpool        gMacGXSpool;
  150.  
  151. #endif
  152.  
  153. //========================================================================================
  154. // class FW_CPrintEnvironment
  155. //========================================================================================
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // FW_CPrintEnvironment::FW_CPrintEnvironment
  159. //----------------------------------------------------------------------------------------
  160.  
  161. FW_CPrintEnvironment::FW_CPrintEnvironment()
  162. {
  163. #ifdef FW_SUPPORT_GX
  164.     if (!FW_IsGXInstalled())
  165.     {
  166. #endif
  167.  
  168. #ifdef FW_BUILD_MAC
  169.         ::PrOpen();                        // Open the current printer driver and resource file
  170.         
  171.         short error = ::PrError();
  172.         if (error == fnfErr)
  173.             FW_Failure(FW_xNoDefaultPrinter);
  174.         FW_FailOnError(error);
  175.  
  176.         fMacResFile = ::CurResFile();    // Save the driver's resource file
  177. #endif
  178.  
  179. #ifdef FW_SUPPORT_GX
  180.     }
  181. #endif
  182.  
  183. #ifdef FW_BUILD_MAC
  184.     ::GetPort(&fOldPort);
  185. #endif
  186.  
  187.     FW_END_CONSTRUCTOR
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // FW_CPrintEnvironment::~FW_CPrintEnvironment
  192. //----------------------------------------------------------------------------------------
  193.  
  194. FW_CPrintEnvironment::~FW_CPrintEnvironment()
  195. {
  196.     FW_START_DESTRUCTOR
  197.  
  198. #ifdef FW_BUILD_MAC
  199.     ::SetPort(fOldPort);
  200. #endif
  201.  
  202. #ifdef FW_SUPPORT_GX
  203.     if (!FW_IsGXInstalled())
  204.     {
  205. #endif
  206.  
  207. #ifdef FW_BUILD_MAC
  208.         ::UseResFile(fMacResFile);
  209.         ::PrClose();
  210. #endif
  211.  
  212. #ifdef FW_SUPPORT_GX
  213.     }
  214. #endif
  215. }
  216.  
  217. #ifdef FW_BUILD_MAC
  218. //----------------------------------------------------------------------------------------
  219. // FW_CPrintEnvironment::MacRestoreResFile
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CPrintEnvironment::MacRestoreResFile()
  223. {
  224.     ::UseResFile(fMacResFile);
  225. }
  226. #endif
  227.  
  228. //========================================================================================
  229. //    class FW_CPrintInfo
  230. //========================================================================================
  231.  
  232. const char*    kPrintInfoProp    =    "ODF:Property:PrintingInfo";
  233.  
  234. #ifdef FW_BUILD_WIN
  235. const char*    kPrintType        =    "Windows:Type:Print info";
  236. const char*    kTypeDevMode    =    "Windows:Type:DEVMODE";
  237. const char*    kTypeDevNames    =    "Windows:Type:DEVNAMES";
  238. #endif
  239.  
  240. #ifdef FW_BUILD_MAC
  241. const char*    kPrintType        =    "Macintosh:Type:TPrint Record";
  242. #endif
  243.  
  244. #ifdef FW_SUPPORT_GX
  245. const char*    kPrintTypeGX    =    "Macintosh:Type:QuickDrawGX Job";
  246. #endif
  247.  
  248. const int kMaxPage = 9999;
  249.  
  250. //----------------------------------------------------------------------------------------
  251. // FW_CPrintInfo::FW_CPrintInfo
  252. //----------------------------------------------------------------------------------------
  253.  
  254. FW_CPrintInfo::FW_CPrintInfo(
  255.         Environment*             ev,
  256.         ODStorageUnit*             storageUnit,
  257.         FW_CPrintEnvironment*    /* printEnv */)
  258. {
  259. #ifdef FW_BUILD_WIN
  260.     WinSetPrintDlgToDefaults();
  261. #endif
  262. #ifdef FW_SUPPORT_GX
  263.     fGXJob = NULL;
  264. #endif
  265. #ifdef FW_BUILD_MAC
  266.     fMacPrintRec = NULL;
  267. #endif
  268.  
  269.     // ----- Check for QuickDraw GX print job
  270.  
  271. #ifdef FW_SUPPORT_GX
  272.     if (FW_IsGXInstalled())
  273.     {
  274.         fGXJob = LoadFromStorageUnitGX(ev, storageUnit);
  275.         if (fGXJob != NULL)
  276.             return;
  277.     }
  278. #endif
  279.  
  280.     // ----- Check for platform print info structure
  281.  
  282. #ifdef FW_BUILD_WIN
  283.     unsigned long bufSize;
  284.     if (::LoadBuffer(ev, storageUnit, kPrintInfoProp, kPrintType, &fWinPrintDlg, bufSize))
  285.     {
  286.         ::LoadHandle(ev, storageUnit, kPrintInfoProp, kTypeDevMode, fWinPrintDlg.hDevMode);
  287.         ::LoadHandle(ev, storageUnit, kPrintInfoProp, kTypeDevNames, fWinPrintDlg.hDevNames);
  288.         return;
  289.     }
  290. #endif
  291.  
  292. #ifdef FW_BUILD_MAC
  293.     THPrint thPrint = NULL;
  294.     if (::LoadHandle(ev, storageUnit, kPrintInfoProp, kPrintType, (FW_PlatformHandle&) thPrint))
  295.     {
  296. #ifdef FW_SUPPORT_GX
  297.         if (FW_IsGXInstalled())
  298.         {
  299.             FW_XPrint::CheckPrintError(::GXNewJob(&fGXJob));
  300.              ::GXConvertPrintRecord(fGXJob, thPrint);
  301.             FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)thPrint);
  302.             FW_XPrint::CheckGXJobError(fGXJob);
  303.             return;
  304.         }
  305. #endif
  306.  
  307.         fMacPrintRec = thPrint;
  308.  
  309.         ::PrValidate(fMacPrintRec);
  310.         FW_XPrint::MacCheckPrintError();
  311.  
  312.         return;
  313.     }
  314. #endif
  315.  
  316.         // ----- Create default print info structure
  317.         
  318. #ifdef FW_SUPPORT_GX
  319.     if (FW_IsGXInstalled())
  320.     {
  321.         FW_XPrint::CheckPrintError(::GXNewJob(&fGXJob));
  322.         return;
  323.     }
  324. #endif
  325.  
  326. #ifdef FW_BUILD_WIN
  327.     WinSetPrintDlgToDefaults();
  328. #endif
  329.  
  330. #ifdef FW_BUILD_MAC
  331.     fMacPrintRec = (THPrint) FW_CMemoryManager::AllocateSystemHandle(sizeof(TPrint));
  332.     ::PrintDefault(fMacPrintRec);
  333. #endif
  334.  
  335.     FW_END_CONSTRUCTOR
  336. }
  337.  
  338. #ifdef FW_SUPPORT_GX
  339. //----------------------------------------------------------------------------------------
  340. // FW_CPrintInfo::LoadFromStorageUnitGX
  341. //----------------------------------------------------------------------------------------
  342.  
  343. gxJob FW_CPrintInfo::LoadFromStorageUnitGX(
  344.         Environment* ev,
  345.         ODStorageUnit* storageUnit)
  346. {
  347.     gxJob job = NULL;
  348.     FW_PlatformHandle handle = NULL;
  349.     if (::LoadHandle(ev, storageUnit, kPrintInfoProp, kPrintTypeGX, handle))
  350.     {
  351.         ::GXNewJob(&job);
  352.         ::GXUnflattenJobFromHdl(job, handle);
  353.         FW_CMemoryManager::FreeSystemHandle(handle);
  354.         FW_XPrint::CheckGXJobError(job);
  355.     }
  356.  
  357.     return job;
  358. }
  359. #endif
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // FW_CPrintInfo::~FW_CPrintInfo
  363. //----------------------------------------------------------------------------------------
  364.  
  365. FW_CPrintInfo::~FW_CPrintInfo()
  366. {
  367.     FW_START_DESTRUCTOR
  368.     
  369. #ifdef FW_SUPPORT_GX
  370.     if (fGXJob != NULL)
  371.         ::GXDisposeJob(fGXJob);
  372. #endif
  373.  
  374. #ifdef FW_BUILD_MAC
  375.     if(fMacPrintRec != NULL)
  376.         FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fMacPrintRec);
  377. #endif
  378. }
  379.  
  380. #ifdef FW_BUILD_WIN
  381. //----------------------------------------------------------------------------------------
  382. // FW_CPrintInfo::WinSetPrintDlgToDefaults
  383. //----------------------------------------------------------------------------------------
  384.  
  385. void FW_CPrintInfo::WinSetPrintDlgToDefaults()
  386. {
  387.     fWinPrintDlg.lStructSize            = sizeof(PRINTDLG);
  388.     fWinPrintDlg.hDevMode                = NULL;
  389.     fWinPrintDlg.hDevNames                = NULL;
  390.     fWinPrintDlg.Flags                    = 0;
  391.     fWinPrintDlg.hwndOwner                = NULL;
  392.     fWinPrintDlg.hDC                     = NULL;
  393.     fWinPrintDlg.nFromPage                = 1;
  394.     fWinPrintDlg.nToPage                = kMaxPage;
  395.     fWinPrintDlg.nMinPage                = 1;
  396.     fWinPrintDlg.nMaxPage                = kMaxPage;
  397.     fWinPrintDlg.nCopies                = 1;
  398.     fWinPrintDlg.hInstance                = NULL;
  399.     fWinPrintDlg.lCustData                = 0l;
  400.     fWinPrintDlg.lpfnPrintHook            = NULL;
  401.     fWinPrintDlg.lpfnSetupHook            = NULL;
  402.     fWinPrintDlg.lpPrintTemplateName    = NULL;
  403.     fWinPrintDlg.lpSetupTemplateName    = NULL;
  404.     fWinPrintDlg.hPrintTemplate            = NULL;
  405.     fWinPrintDlg.hSetupTemplate            = NULL;
  406. }
  407. #endif
  408.  
  409. //----------------------------------------------------------------------------------------
  410. // FW_CPrintInfo::SaveToStorageUnit
  411. //----------------------------------------------------------------------------------------
  412.  
  413. void FW_CPrintInfo::SaveToStorageUnit(Environment* ev, ODStorageUnit* storageUnit) const
  414. {
  415. #ifdef FW_SUPPORT_GX
  416.     if (fGXJob != NULL)
  417.     {
  418.         FW_PlatformHandle handle = FW_CMemoryManager::AllocateSystemHandle(0);
  419.         ::GXFlattenJobToHdl(fGXJob, handle);
  420.  
  421.         FW_TRY
  422.         {
  423.             ::SaveHandle(ev, storageUnit, kPrintInfoProp, kPrintTypeGX, handle);
  424.         }
  425.         FW_CATCH_BEGIN
  426.         FW_CATCH_EVERYTHING()
  427.         {
  428.             FW_CMemoryManager::FreeSystemHandle(handle);
  429.     
  430.             FW_THROW_SAME();
  431.         }
  432.         FW_CATCH_END
  433.  
  434.         FW_CMemoryManager::FreeSystemHandle(handle);
  435.         return;
  436.     }
  437. #endif
  438.  
  439. #ifdef FW_BUILD_WIN
  440.     ::SaveBuffer(ev, storageUnit, kPrintInfoProp, kPrintType, &fWinPrintDlg, sizeof fWinPrintDlg);
  441.     
  442.     ::SaveHandle(ev, storageUnit, kPrintInfoProp, kTypeDevNames, fWinPrintDlg.hDevNames);
  443.     ::SaveHandle(ev, storageUnit, kPrintInfoProp, kTypeDevMode, fWinPrintDlg.hDevMode);
  444. #endif
  445. #ifdef FW_BUILD_MAC
  446.     ::SaveHandle(ev, storageUnit, kPrintInfoProp, kPrintType, (FW_PlatformHandle)fMacPrintRec);
  447. #endif
  448. }
  449.  
  450. //----------------------------------------------------------------------------------------
  451. // FW_CPrintInfo::GetCopyCount
  452. //----------------------------------------------------------------------------------------
  453.  
  454. void FW_CPrintInfo::GetCopyCount(short& copyCount, FW_Boolean& collate) const
  455. {
  456. #ifdef FW_SUPPORT_GX
  457.     if (fGXJob != NULL)
  458.     {
  459.         copyCount = 1;
  460.         collate   = FALSE;
  461.         return;
  462.     }
  463. #endif
  464. #ifdef FW_BUILD_WIN
  465.     copyCount    = fWinPrintDlg.nCopies;
  466.     collate        = (fWinPrintDlg.Flags & PD_COLLATE) != 0;
  467. #endif
  468. #ifdef FW_BUILD_MAC
  469.     copyCount    = (*fMacPrintRec)->prJob.iCopies;
  470.     collate        = FALSE;
  471. #endif
  472. }
  473.  
  474. //----------------------------------------------------------------------------------------
  475. // FW_CPrintInfo::GetPages
  476. //----------------------------------------------------------------------------------------
  477.  
  478. void FW_CPrintInfo::GetPages(long& firstPage, long& lastPage)
  479. {
  480. #ifdef FW_SUPPORT_GX
  481.     if (fGXJob != NULL)
  482.     {
  483.         ::GXGetJobPageRange(fGXJob, &firstPage, &lastPage);
  484.         return;
  485.     }
  486. #endif
  487. #ifdef FW_BUILD_WIN
  488.     firstPage    = fWinPrintDlg.nFromPage;
  489.     lastPage    = fWinPrintDlg.nToPage;
  490. #endif
  491. #ifdef FW_BUILD_MAC
  492.     firstPage    = (*fMacPrintRec)->prJob.iFstPage;
  493.     lastPage    = (*fMacPrintRec)->prJob.iLstPage;
  494. #endif
  495. }
  496.  
  497. //----------------------------------------------------------------------------------------
  498. // FW_CPrintInfo::GetPageBounds
  499. //----------------------------------------------------------------------------------------
  500.  
  501. FW_CRect FW_CPrintInfo::GetPageBounds() const
  502. {
  503. #ifdef FW_SUPPORT_GX
  504.     if (fGXJob != NULL)
  505.     {
  506.         gxFormat format = ::GXGetJobFormat(fGXJob, 1);
  507.         gxRectangle pageBounds;
  508.         ::GXGetFormatDimensions(format, &pageBounds, nil);
  509.         
  510.         return FW_CRect(
  511.             FW_ODFixedToFixed(pageBounds.left),
  512.             FW_ODFixedToFixed(pageBounds.top),
  513.             FW_ODFixedToFixed(pageBounds.right),
  514.             FW_ODFixedToFixed(pageBounds.bottom));
  515.     }
  516. #endif
  517. #ifdef FW_BUILD_WIN
  518.     // Scale the bounds
  519.     FW_CPoint resPrint(
  520.         FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX)),
  521.         FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY)));
  522.  
  523.     FW_CRect pageBounds = fWinPageBounds;
  524.  
  525.     pageBounds.left        = FW_WideMultiply(pageBounds.left,        FW_kFixed72) / resPrint.x;
  526.     pageBounds.top        = FW_WideMultiply(pageBounds.top,        FW_kFixed72) / resPrint.y;
  527.     pageBounds.right    = FW_WideMultiply(pageBounds.right,        FW_kFixed72) / resPrint.x;
  528.     pageBounds.bottom    = FW_WideMultiply(pageBounds.bottom,    FW_kFixed72) / resPrint.y;
  529.  
  530.     return pageBounds;
  531. #endif
  532. #ifdef FW_BUILD_MAC
  533.     return (*fMacPrintRec)->prInfo.rPage;
  534. #endif
  535. }
  536.  
  537. //----------------------------------------------------------------------------------------
  538. // FW_CPrintInfo::GetPaperBounds
  539. //----------------------------------------------------------------------------------------
  540.  
  541. FW_CRect FW_CPrintInfo::GetPaperBounds() const
  542. {
  543. #ifdef FW_SUPPORT_GX
  544.     if (fGXJob != NULL)
  545.     {
  546.         gxFormat format = ::GXGetJobFormat(fGXJob, 1);
  547.         gxRectangle paperBounds;
  548.         ::GXGetFormatDimensions(format, nil, &paperBounds);
  549.         
  550.         return FW_CRect(
  551.             FW_ODFixedToFixed(paperBounds.left),
  552.             FW_ODFixedToFixed(paperBounds.top),
  553.             FW_ODFixedToFixed(paperBounds.right),
  554.             FW_ODFixedToFixed(paperBounds.bottom));
  555.     }
  556. #endif
  557. #ifdef FW_BUILD_WIN
  558.     // Scale the bounds
  559.     FW_CPoint resPrint(
  560.         FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX)),
  561.         FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY)));
  562.  
  563.     FW_CRect pageBounds = fWinPageBounds;
  564.  
  565.     pageBounds.left        = FW_WideMultiply(pageBounds.left,        FW_kFixed72) / resPrint.x;
  566.     pageBounds.top        = FW_WideMultiply(pageBounds.top,        FW_kFixed72) / resPrint.y;
  567.     pageBounds.right    = FW_WideMultiply(pageBounds.right,        FW_kFixed72) / resPrint.x;
  568.     pageBounds.bottom    = FW_WideMultiply(pageBounds.bottom,    FW_kFixed72) / resPrint.y;
  569.  
  570.     return pageBounds;
  571. #endif
  572. #ifdef FW_BUILD_MAC
  573.     return (*fMacPrintRec)->rPaper;
  574. #endif
  575. }
  576.  
  577. //----------------------------------------------------------------------------------------
  578. // FW_CPrintInfo::GetPrinterResolution
  579. //----------------------------------------------------------------------------------------
  580.  
  581. FW_Boolean FW_CPrintInfo::GetPrinterResolution(long& hRes, long& vRes) const
  582. {
  583. #ifdef FW_SUPPORT_GX
  584.  
  585.     if (fGXJob != NULL)
  586.     {
  587.         gxFormat format = ::GXGetJobFormat(fGXJob, 1);
  588.  
  589.         gxMapping mapping;
  590.         ::GXGetFormatMapping(format, &mapping);
  591.         
  592.         // Fixed * long => long
  593.         hRes = FixMul(mapping.map[0][0], 72L);
  594.         vRes = FixMul(mapping.map[1][1], 72L);
  595.  
  596.         return ::GXGetJobError(fGXJob) == noErr;
  597.     }
  598.     
  599. #endif
  600. #ifdef FW_BUILD_MAC
  601.  
  602.     hRes = (*fMacPrintRec)->prInfo.iHRes;
  603.     vRes = (*fMacPrintRec)->prInfo.iVRes;
  604.     return true;
  605.  
  606. #endif
  607. #ifdef FW_BUILD_WIN
  608.  
  609.     hRes = ::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX);
  610.     vRes = ::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY);
  611.     return true;
  612.     
  613. #endif
  614. }
  615.  
  616. //----------------------------------------------------------------------------------------
  617. // FW_CPrintInfo::SetPrinterResolution
  618. //----------------------------------------------------------------------------------------
  619.  
  620. FW_Boolean FW_CPrintInfo::SetPrinterResolution(long hRes, long vRes)
  621. {
  622.     long curHRes, curVRes;
  623.  
  624.     if(GetPrinterResolution(curHRes, curVRes) && hRes == curHRes && vRes == curVRes)
  625.         return true;
  626.  
  627.     FW_DEBUG_MESSAGE("FW_CPrintInfo::SetPrinterResolution not implemented");
  628.     return false;        
  629. }
  630.  
  631.  
  632. #ifdef FW_BUILD_WIN
  633. //----------------------------------------------------------------------------------------
  634. // FW_CPrintInfo::GetGXJob
  635. //----------------------------------------------------------------------------------------
  636.  
  637. LPPRINTDLG FW_CPrintInfo::WinGetPrintDlg()
  638. {
  639.     return &fWinPrintDlg;
  640. }
  641. #endif
  642.  
  643. #ifdef FW_BUILD_WIN
  644. //----------------------------------------------------------------------------------------
  645. // FW_CPrintInfo::GetGXJob
  646. //----------------------------------------------------------------------------------------
  647.  
  648. void FW_CPrintInfo::WinUpdatePageRect()
  649. {
  650.     FW_ASSERT(fWinPrintDlg.hDC != NULL);
  651.     ::GetClipBox(fWinPrintDlg.hDC, &fWinPageBounds);
  652. }
  653. #endif
  654.  
  655. #ifdef FW_SUPPORT_GX
  656. //----------------------------------------------------------------------------------------
  657. // FW_CPrintInfo::GetGXJob
  658. //----------------------------------------------------------------------------------------
  659.  
  660. gxJob FW_CPrintInfo::GetGXJob() const
  661. {
  662.     return fGXJob;
  663. }
  664. #endif
  665.  
  666. #ifdef FW_BUILD_MAC
  667. //----------------------------------------------------------------------------------------
  668. // FW_CPrintInfo::MacGetTHPrint
  669. //----------------------------------------------------------------------------------------
  670.  
  671. THPrint FW_CPrintInfo::MacGetTHPrint() const
  672. {
  673.     return fMacPrintRec;
  674. }
  675. #endif
  676.  
  677. //========================================================================================
  678. // class FW_CPrintJob
  679. //========================================================================================
  680.  
  681. //----------------------------------------------------------------------------------------
  682. // FW_CPrintJob::FW_CPrintJob
  683. //----------------------------------------------------------------------------------------
  684.  
  685. FW_CPrintJob::FW_CPrintJob(
  686.         FW_CPrintInfo*            printInfo,
  687.         FW_CPrintHandler*        printHandler,
  688.         FW_CPrintEnvironment*    printEnv) :
  689.     fPrintInfo(printInfo),
  690.     fPrintHandler(printHandler),
  691.     fPrintEnv(printEnv)
  692. {
  693. #ifdef FW_SUPPORT_GX
  694.     fGXViewPort            = NULL;
  695.     fGXSpoolUPP            = NULL;
  696.     fMacCanvasWindow    = NULL;
  697. #endif
  698. #ifdef FW_BUILD_WIN
  699.     fWinSaveDCIndex        = 0;
  700. #endif
  701. #ifdef FW_BUILD_MAC
  702.     fMacPrPort            = NULL;
  703. #endif
  704. }
  705.  
  706. //----------------------------------------------------------------------------------------
  707. // FW_CPrintJob::~FW_CPrintJob
  708. //----------------------------------------------------------------------------------------
  709.  
  710. FW_CPrintJob::~FW_CPrintJob()
  711. {
  712. #ifdef FW_SUPPORT_GX
  713.     FW_ASSERT(fGXViewPort == NULL);
  714. #endif
  715. #ifdef FW_BUILD_MAC
  716.     FW_ASSERT(fMacPrPort == NULL);
  717. #endif
  718. }
  719.  
  720. //----------------------------------------------------------------------------------------
  721. // FW_CPrintJob::OpenDocument
  722. //----------------------------------------------------------------------------------------
  723.  
  724. ODFacet* FW_CPrintJob::OpenDocument(Environment*        ev,
  725.                                     ODFrame*            frame,
  726.                                     long                numPages,
  727.                                     const FW_CString&    title)
  728. {
  729.     FW_ASSERT(numPages > 0);
  730.     
  731.     ODFacet* facet = NULL;
  732.     
  733.     // Create a progress dialog
  734.     fPrintHandler->CreateProgressDialog(ev, frame, numPages, title);
  735.  
  736.     FW_TRY
  737.     {
  738.         // Create the platform print job
  739.         PrivCreatePlatformPrintJob(numPages, title);
  740.  
  741.         FW_TRY
  742.         {
  743.             // Get the transform and shape for the page facet
  744.             FW_CRect                pageBounds    = fPrintInfo->GetPageBounds();
  745.             FW_CAcquiredODTransform    aqTransform    = frame->CreateTransform(ev);
  746.             FW_CAcquiredODShape        aqClipShape    = frame->CreateShape(ev);
  747.  
  748.             aqClipShape->SetRectangle(ev, (ODRect*) &pageBounds);
  749.  
  750.             // Create a facet with this geometry
  751.             facet = FW_CSession::GetWindowState(ev)->CreateFacet(ev, frame, aqClipShape,
  752.                                                                             aqTransform, kODNULL, kODNULL);
  753.             FW_ASSERT(facet != NULL);
  754.  
  755.             FW_TRY
  756.             {
  757.                 // Create an OpenDoc canvas
  758.                 ODCanvas* canvas = PrivCreatePrintCanvas(ev, facet);
  759.                 FW_ASSERT(canvas != NULL);
  760.  
  761.                 // Set its owner
  762.                 FW_CAcquiredODPart aqPart = frame->AcquirePart(ev);
  763.                 canvas->SetOwner(ev, aqPart);
  764.  
  765.                 FW_TRY
  766.                 {
  767.                     // Attach the canvas to the facet
  768.                     facet->ChangeCanvas(ev, canvas);
  769.  
  770.                     FW_TRY
  771.                     {
  772.                         // Add the facet to the frame
  773.                         frame->FacetAdded(ev, facet);
  774.                     }
  775.                     FW_CATCH_BEGIN
  776.                     FW_CATCH_EVERYTHING()
  777.                     {
  778.                         // If the canvas has already been attached to the facet, then
  779.                         // we need to delete the facet first to prevent the facet from
  780.                         // referencing a canvas that has been deleted
  781.                         delete facet;
  782.                         facet = NULL;
  783.                         delete canvas;
  784.                         canvas = NULL;
  785.                         FW_THROW_SAME();
  786.                     }
  787.                     FW_CATCH_END
  788.                 }
  789.                 FW_CATCH_BEGIN
  790.                 FW_CATCH_EVERYTHING()
  791.                 {
  792.                     delete canvas;
  793.                     FW_THROW_SAME();
  794.                 }
  795.                 FW_CATCH_END
  796.             }
  797.             FW_CATCH_BEGIN
  798.             FW_CATCH_EVERYTHING()
  799.             {
  800.                 delete facet;
  801.                 FW_THROW_SAME();
  802.             }
  803.             FW_CATCH_END
  804.         }
  805.         FW_CATCH_BEGIN
  806.         FW_CATCH_EVERYTHING()
  807.         {
  808.             PrivAbortPlatformPrintJob();
  809.             FW_THROW_SAME();
  810.         }
  811.         FW_CATCH_END
  812.     }
  813.     FW_CATCH_BEGIN
  814.     FW_CATCH_EVERYTHING()
  815.     {
  816.         fPrintHandler->DestroyProgressDialog(ev, frame);
  817.  
  818.         FW_THROW_SAME();
  819.     }
  820.     FW_CATCH_END
  821.  
  822.     return facet;
  823. }
  824.  
  825. //----------------------------------------------------------------------------------------
  826. // FW_CPrintJob::PrivCreatePlatformPrintJob
  827. //----------------------------------------------------------------------------------------
  828.  
  829. void FW_CPrintJob::PrivCreatePlatformPrintJob(long    numPages, const FW_CString&    title)
  830. {
  831. #ifdef FW_SUPPORT_GX
  832.     gxJob job = fPrintInfo->GetGXJob();
  833.     if (job != NULL)
  834.     {
  835.         Str255 pascalTitle;
  836.         title.ExportPascal(pascalTitle);
  837.     
  838.         ::GXStartJob(job, pascalTitle, numPages);
  839.         FW_XPrint::CheckGXJobError(job);
  840.         
  841.         return;
  842.     }
  843. #endif
  844.  
  845. #ifdef FW_BUILD_WIN
  846.     DOCINFO di;
  847.  
  848.     di.cbSize        = sizeof(DOCINFO);
  849.     di.lpszDocName    = title;
  850.     di.lpszOutput    = NULL;
  851.     di.lpszDatatype    = NULL;
  852.     di.fwType        = 0;
  853.  
  854.     LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  855.     FW_ASSERT(lppd->hDC != NULL);
  856.     FW_XPrint::CheckPrintError(::StartDoc(lppd->hDC, &di));
  857. #endif
  858.  
  859. #ifdef FW_BUILD_MAC
  860.     fPrintEnv->MacRestoreResFile();
  861.  
  862.     THPrint thPrint = fPrintInfo->MacGetTHPrint();
  863.     ::PrValidate(thPrint);    // Set document name
  864.  
  865.     {
  866.         FW_ASSERT(fMacPrPort == NULL);
  867.         FW_CMacTempPort tempPort;
  868.  
  869.         fMacPrPort = ::PrOpenDoc(thPrint, NULL, NULL);
  870.         FW_XPrint::MacCheckPrintError();
  871.         FW_ASSERT(fMacPrPort != NULL);
  872.     }
  873. #endif
  874. }
  875.  
  876. //----------------------------------------------------------------------------------------
  877. // FW_CPrintJob::PrivAbortPlatformPrintJob
  878. //----------------------------------------------------------------------------------------
  879.  
  880. void FW_CPrintJob::PrivAbortPlatformPrintJob()
  881. {
  882. #ifdef FW_SUPPORT_GX
  883.     gxJob job = fPrintInfo->GetGXJob();
  884.     if (job != NULL)
  885.     {
  886.         // Clean up QD -> GX spooling structures
  887.         ::DisposeWindow(fMacCanvasWindow);
  888.         fMacCanvasWindow = NULL;
  889.  
  890.         DisposeRoutineDescriptor(fGXSpoolUPP);
  891.         
  892.         ::GXSetJobError(job, gxPrUserAbortErr);
  893.         ::GXFinishJob(job);
  894.         ::GXDisposeViewPort(fGXViewPort);
  895.         fGXViewPort = NULL;
  896.         
  897.         return;
  898.     }
  899. #endif
  900. #ifdef FW_BUILD_WIN
  901.     LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  902.     FW_ASSERT(lppd->hDC != NULL);
  903.  
  904.     // Abort the print job
  905.     int error = ::AbortDoc(lppd->hDC);
  906.     ::DeleteDC(lppd->hDC);
  907.     lppd->hDC = NULL;
  908. #endif
  909. #ifdef FW_BUILD_MAC
  910.     FW_ASSERT(fMacPrPort != NULL);
  911.     FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
  912.  
  913.     fPrintEnv->MacRestoreResFile();
  914.  
  915.     ::PrSetError(iPrAbort);
  916.     ::PrCloseDoc(fMacPrPort);
  917.     fMacPrPort = NULL;
  918. #endif
  919. }
  920.  
  921. //----------------------------------------------------------------------------------------
  922. // FW_CPrintJob::PrivCreatePrintCanvas
  923. //----------------------------------------------------------------------------------------
  924.  
  925. ODCanvas* FW_CPrintJob::PrivCreatePrintCanvas(Environment* ev, ODFacet* facet)
  926. {
  927.     ODCanvas* canvas;
  928.  
  929. #ifdef FW_SUPPORT_GX
  930.     gxJob job = fPrintInfo->GetGXJob();
  931.     if (job != NULL)
  932.     {
  933.         fGXViewPort = ::GXNewViewPort(gxScreenViewDevices);
  934.  
  935.         // DTC -- Coercing fGXViewPort distresses the compier since gxViewPort is a pointer
  936.         // to a partially defined struct.  This is OK, the Apple headers partially define
  937.         // it in order to prevent direct access.
  938. #if (defined __SC__ || defined __MRC__) && !defined _WINDOWS
  939. #pragma options(!warn_cast_incomplete_type)    // pragma will be in effect for remainder of function
  940. #endif
  941.         canvas = facet->CreateCanvas(ev, kODQuickDrawGX,
  942.                                 (ODPlatformCanvas) fGXViewPort, kODFalse, kODFalse);
  943.         FW_ASSERT(canvas != NULL);
  944.     
  945.         gxJob job = fPrintInfo->GetGXJob();
  946.         FW_ASSERT(job != NULL);
  947.         canvas->SetPlatformPrintJob(ev, kODQuickDrawGX, job);
  948.     
  949.         // Create a new QuickDraw window to draw to
  950.         FW_CPlatformRect boundsWindow = fPrintInfo->GetPageBounds();
  951.         fMacCanvasWindow = ::NewCWindow(NULL, &boundsWindow, "\p", FALSE,
  952.             documentProc, NULL, FALSE, 0l);
  953.     
  954.         // Create a UPP for the spool proc
  955.         fGXSpoolUPP = NewgxShapeSpoolProc(MacGXSpoolProc);
  956.     
  957.         // Set the QuickDraw canvas in the OD Canvas
  958.         canvas->SetPlatformCanvas(ev, kODQuickDraw, fMacCanvasWindow);
  959.         return canvas;
  960.     }
  961. #endif
  962.  
  963. #ifdef FW_BUILD_WIN
  964.     LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  965.     canvas = facet->CreateCanvas(ev, kODWindows,
  966.                                 lppd->hDC, kODFalse, kODFalse);
  967.     canvas->SetPlatformPrintJob(ev, kODWindows, lppd);
  968. #endif
  969. #ifdef FW_BUILD_MAC
  970.     canvas = facet->CreateCanvas(ev, kODQuickDraw,
  971.                                 (ODPlatformCanvas) fMacPrPort, kODFalse, kODFalse);
  972.  
  973.     THPrint thPrint = fPrintInfo->MacGetTHPrint();
  974.     FW_ASSERT(thPrint != NULL);
  975.  
  976.     canvas->SetPlatformPrintJob(ev, kODQuickDraw, thPrint);
  977. #endif
  978.  
  979.     return canvas;
  980. }
  981.  
  982. //----------------------------------------------------------------------------------------
  983. // FW_CPrintJob::CloseDocument
  984. //----------------------------------------------------------------------------------------
  985.  
  986. void FW_CPrintJob::CloseDocument(
  987.     Environment*        ev,
  988.     ODFacet*            facet)
  989. {
  990.     DoCloseDocument(ev, facet, TRUE);
  991. }
  992.  
  993. //----------------------------------------------------------------------------------------
  994. // FW_CPrintJob::CloseDocumentOnError
  995. //----------------------------------------------------------------------------------------
  996.  
  997. void FW_CPrintJob::CloseDocumentOnError(
  998.     Environment*        ev,
  999.     ODFacet*            facet)
  1000. {
  1001.     DoCloseDocument(ev, facet, FALSE);
  1002. }
  1003.  
  1004. //----------------------------------------------------------------------------------------
  1005. // FW_CPrintJob::OpenPage
  1006. //----------------------------------------------------------------------------------------
  1007.  
  1008. void FW_CPrintJob::OpenPage(long pageNumber)
  1009. {
  1010.     FW_ASSERT(pageNumber > 0);
  1011.  
  1012. #ifdef FW_SUPPORT_GX
  1013.     gxJob job = fPrintInfo->GetGXJob();
  1014.     if (job != NULL)
  1015.     {
  1016.         // Start a new page
  1017.         ::GXStartPage(job, pageNumber, ::GXGetJobFormat(job, 1), 1, &fGXViewPort);
  1018.         FW_XPrint::CheckGXJobError(job);
  1019.  
  1020.         // Install QuickDraw -> QuickDraw GX translator
  1021.         gMacGXSpool.fGXViewPort = fGXViewPort;
  1022.         gMacGXSpool.fGXPrintJob = job;
  1023.         gMacGXSpool.fPageBounds = fPrintInfo->GetPageBounds();
  1024.  
  1025.         FW_CPlatformRect    pageRect = gMacGXSpool.fPageBounds;
  1026.         FW_CPlatformPoint    styleStretch(1, 1);
  1027.         
  1028.         FW_ASSERT(fMacCanvasWindow != NULL);
  1029.         FW_ASSERT(fGXSpoolUPP != NULL);
  1030.  
  1031.         ::GXInstallQDTranslator(fMacCanvasWindow,
  1032.                               gxDefaultOptionsTranslation,
  1033.                               &pageRect, &pageRect,
  1034.                               styleStretch, 
  1035.                               fGXSpoolUPP,
  1036.                               NULL);
  1037.  
  1038.         return;
  1039.     }
  1040. #endif
  1041. #ifdef FW_BUILD_WIN
  1042.     LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  1043.     FW_ASSERT(lppd->hDC != NULL);
  1044.     FW_XPrint::CheckPrintError(::StartPage(lppd->hDC));
  1045.  
  1046.     // Restore the DC
  1047.     if (fWinSaveDCIndex != 0)
  1048.     {
  1049.         ::RestoreDC(lppd->hDC, fWinSaveDCIndex);
  1050.         fWinSaveDCIndex = 0;
  1051.     }
  1052. #endif
  1053. #ifdef FW_BUILD_MAC
  1054.     {
  1055.         FW_ASSERT(fMacPrPort != NULL);
  1056.         FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
  1057.  
  1058.         fPrintEnv->MacRestoreResFile();
  1059.  
  1060.         ::PrOpenPage(fMacPrPort, NULL);
  1061.  
  1062.         FW_XPrint::MacCheckPrintError();
  1063.     }
  1064. #endif
  1065.  
  1066.     // Show current page number
  1067.     fPrintHandler->UpdateProgressDialog(pageNumber);
  1068. }
  1069.  
  1070. //----------------------------------------------------------------------------------------
  1071. // FW_CPrintJob::ClosePage
  1072. //----------------------------------------------------------------------------------------
  1073.  
  1074. void FW_CPrintJob::ClosePage()
  1075. {
  1076.     DoClosePage(TRUE);
  1077. }
  1078.  
  1079. //----------------------------------------------------------------------------------------
  1080. // FW_CPrintJob::ClosePageOnError
  1081. //----------------------------------------------------------------------------------------
  1082.  
  1083. void FW_CPrintJob::ClosePageOnError()
  1084. {
  1085.     DoClosePage(FALSE);
  1086. }
  1087.  
  1088. //----------------------------------------------------------------------------------------
  1089. // FW_CPrintJob::FinishPrinting
  1090. //----------------------------------------------------------------------------------------
  1091.  
  1092. void FW_CPrintJob::FinishPrinting()
  1093. {
  1094. #ifdef FW_SUPPORT_GX
  1095.     gxJob job = fPrintInfo->GetGXJob();
  1096.     if (job != NULL)
  1097.         return;
  1098. #endif
  1099.  
  1100. #ifdef FW_BUILD_MAC
  1101.     FW_ASSERT(fMacPrPort == NULL);
  1102.  
  1103.     THPrint thPrint = fPrintInfo->MacGetTHPrint();
  1104.     if ((*thPrint)->prJob.bJDocLoop == bSpoolLoop)
  1105.     {
  1106.         TPrStatus printStatus;
  1107.         ::PrPicFile(thPrint, NULL, NULL, NULL, &printStatus);
  1108.     }
  1109. #endif
  1110. }
  1111.  
  1112. //----------------------------------------------------------------------------------------
  1113. // FW_CPrintJob::DoCloseDocument
  1114. //----------------------------------------------------------------------------------------
  1115.  
  1116. void FW_CPrintJob::DoCloseDocument(
  1117.             Environment*        ev,
  1118.             ODFacet*            facet,
  1119.             FW_Boolean            checkErrors)
  1120. {
  1121.     ODCanvas* canvas = facet->GetCanvas(ev);
  1122.     ODFrame* frame = facet->GetFrame(ev);
  1123.  
  1124.     frame->FacetRemoved(ev, facet);
  1125.     delete canvas;
  1126.     delete facet;
  1127.  
  1128.     FW_TRY
  1129.     {
  1130. #ifdef FW_SUPPORT_GX
  1131.         gxJob job = fPrintInfo->GetGXJob();
  1132.         if (job != NULL)
  1133.         {
  1134.             // Finish the job
  1135.             ::GXFinishJob(job);
  1136.  
  1137.             // Clean up QD -> GX spooling structures
  1138.             ::DisposeWindow(fMacCanvasWindow);
  1139.             fMacCanvasWindow = NULL;
  1140.             DisposeRoutineDescriptor(fGXSpoolUPP);
  1141.  
  1142.             // Dispose the viewport
  1143.             ::GXDisposeViewPort(fGXViewPort);
  1144.             fGXViewPort = NULL;
  1145.  
  1146.             if (checkErrors)
  1147.                 FW_XPrint::CheckGXJobError(job);
  1148.         }
  1149.         else
  1150. #endif
  1151.         {
  1152. #ifdef FW_BUILD_WIN
  1153.             LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  1154.             FW_ASSERT(lppd->hDC != NULL);
  1155.         
  1156.             // Finish the print job
  1157.             int error = ::EndDoc(lppd->hDC);
  1158.             ::DeleteDC(lppd->hDC);
  1159.             lppd->hDC = NULL;
  1160.             if (checkErrors)
  1161.                 FW_XPrint::CheckPrintError(error);
  1162. #endif
  1163. #ifdef FW_BUILD_MAC
  1164.             fPrintEnv->MacRestoreResFile();
  1165.             
  1166.             {    
  1167.                 FW_ASSERT(fMacPrPort != NULL);
  1168.                 FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
  1169.                 
  1170.                 ::PrCloseDoc(fMacPrPort);
  1171.                 fMacPrPort = NULL;
  1172.             }
  1173.         
  1174.             if (checkErrors)
  1175.                 FW_XPrint::MacCheckPrintError();
  1176. #endif
  1177.         }
  1178.     }
  1179.     FW_CATCH_BEGIN
  1180.     FW_CATCH_EVERYTHING()
  1181.     {
  1182.         // Close the progress dialog
  1183.         fPrintHandler->DestroyProgressDialog(ev, frame);
  1184.         FW_THROW_SAME();
  1185.     }
  1186.     FW_CATCH_END
  1187.  
  1188.     // Close the progress dialog
  1189.     fPrintHandler->DestroyProgressDialog(ev, frame);
  1190. }
  1191.  
  1192. //----------------------------------------------------------------------------------------
  1193. // FW_CPrintJob::DoClosePage
  1194. //----------------------------------------------------------------------------------------
  1195.  
  1196. void FW_CPrintJob::DoClosePage(
  1197.             FW_Boolean            checkErrors)
  1198. {
  1199. #ifdef FW_SUPPORT_GX
  1200.     gxJob job = fPrintInfo->GetGXJob();
  1201.     if (job != NULL)
  1202.     {
  1203.         // Remove the QD->GX translator; this flushes the GX shape queue
  1204.         gxTranslationStatistic statistic;
  1205.         ::GXRemoveQDTranslator(fMacCanvasWindow, &statistic);
  1206.  
  1207.         // Finish the page
  1208.         ::GXFinishPage(job);
  1209.  
  1210.         if (checkErrors)
  1211.             FW_XPrint::CheckGXJobError(job);
  1212.  
  1213.         return;
  1214.     }
  1215. #endif
  1216. #ifdef FW_BUILD_WIN
  1217.     LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
  1218.     FW_ASSERT(lppd->hDC != NULL);
  1219.  
  1220.     // Save the DC so it can be restored at the start of next page
  1221.     fWinSaveDCIndex = ::SaveDC(lppd->hDC);
  1222.  
  1223.     int error = ::EndPage(lppd->hDC);
  1224.     if (checkErrors)
  1225.         FW_XPrint::CheckPrintError(error);
  1226. #endif
  1227. #ifdef FW_BUILD_MAC
  1228.     {
  1229.         FW_ASSERT(fMacPrPort != NULL);
  1230.         FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
  1231.  
  1232.         fPrintEnv->MacRestoreResFile();
  1233.  
  1234.         ::PrClosePage(fMacPrPort);
  1235.     }
  1236.  
  1237.     if (checkErrors)
  1238.         FW_XPrint::MacCheckPrintError();
  1239. #endif
  1240. }
  1241.  
  1242. //----------------------------------------------------------------------------------------
  1243. // FW_XPrint::CheckPrintError
  1244. //----------------------------------------------------------------------------------------
  1245.  
  1246. void FW_XPrint::CheckPrintError(FW_PlatformError error)
  1247. {
  1248. #ifdef FW_SUPPORT_GX
  1249.     if (error == gxPrUserAbortErr)
  1250.         FW_Failure(FW_xPrintingCanceled);
  1251. #endif
  1252. #ifdef FW_BUILD_WIN
  1253.     if (error == SP_USERABORT)
  1254.         FW_Failure(FW_xPrintingCanceled);
  1255.  
  1256.     if (error < 0)
  1257.         FW_Failure(error);
  1258. #endif
  1259. #ifdef FW_BUILD_MAC
  1260.     if (error == iPrAbort)
  1261.         FW_Failure(FW_xPrintingCanceled);
  1262.  
  1263.     if(error != 0)
  1264.         FW_Failure(error);
  1265. #endif
  1266. }
  1267.  
  1268. #ifdef FW_SUPPORT_GX
  1269. //----------------------------------------------------------------------------------------
  1270. // FW_XPrint::CheckGXJobError
  1271. //----------------------------------------------------------------------------------------
  1272.  
  1273. void FW_XPrint::CheckGXJobError(gxJob job)
  1274. {
  1275.     CheckPrintError(::GXGetJobError(job));
  1276. }
  1277. #endif
  1278.  
  1279. #ifdef FW_BUILD_MAC
  1280. //----------------------------------------------------------------------------------------
  1281. // FW_XPrint::MacCheckPrintError
  1282. //----------------------------------------------------------------------------------------
  1283.  
  1284. void FW_XPrint::MacCheckPrintError()
  1285. {
  1286.     short error = ::PrError();
  1287.     CheckPrintError(error);
  1288. }
  1289. #endif
  1290.  
  1291. #if defined(FW_SUPPORT_GX) && defined(FW_BUILD_MAC)
  1292.  
  1293. //----------------------------------------------------------------------------------------
  1294. // MacGXSpoolProc
  1295. //----------------------------------------------------------------------------------------
  1296.  
  1297. OSErr    MacGXSpoolProc(gxShape shape, long /* refCon */)
  1298. {
  1299.     gxShapeType        shapeType = ::GXGetShapeType(shape);
  1300.     
  1301.     if (shapeType == gxEmptyType   || 
  1302.         shapeType == gxFullType    || 
  1303.         shapeType == gxPictureType ||
  1304.         ::GXTouchesBoundsShape((gxRectangle*)&gMacGXSpool.fPageBounds, shape))
  1305.     {
  1306.         ::GXSetShapeViewPorts(shape, 1, &gMacGXSpool.fGXViewPort);
  1307.         ::GXDrawShape(shape);
  1308.         ::GXSetShapeViewPorts(shape, 0, NULL);
  1309.     }
  1310.  
  1311.     return (OSErr) ::GXGetGraphicsError(NULL);
  1312. }
  1313. #endif
  1314.  
  1315. //----------------------------------------------------------------------------------------
  1316. // SaveHandle
  1317. //----------------------------------------------------------------------------------------
  1318.  
  1319. void    SaveHandle(
  1320.             Environment*         ev,
  1321.             ODStorageUnit*         storageUnit,
  1322.             const char*            propName,
  1323.             const char*            propType,
  1324.             FW_PlatformHandle    handle)
  1325. {
  1326.     if (handle == NULL)
  1327.     {
  1328.         if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
  1329.         {
  1330.             storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
  1331.             storageUnit->Remove(ev);
  1332.         }
  1333.     }
  1334.     else
  1335.     {
  1336.         FW_CAcquireLockedSystemHandle lock(handle);
  1337.     
  1338.         void* buffer             = lock.GetPointer();
  1339.         unsigned long bufSize    = FW_CMemoryManager::GetSystemHandleSize(handle);
  1340.     
  1341.         ::SaveBuffer(ev, storageUnit, propName, propType, buffer, bufSize);
  1342.     }
  1343. }
  1344.             
  1345. //----------------------------------------------------------------------------------------
  1346. // SaveBuffer
  1347. //----------------------------------------------------------------------------------------
  1348.  
  1349. void    SaveBuffer(
  1350.             Environment*         ev,
  1351.             ODStorageUnit*         storageUnit,
  1352.             const char*            propName,
  1353.             const char*            propType,
  1354.             const void*            buffer,
  1355.             unsigned long        bufSize)
  1356. {
  1357.     if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
  1358.         storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
  1359.     else
  1360.         storageUnit->AddProperty(ev, (ODType) propName)->AddValue(ev, (ODType) propType);
  1361.  
  1362.     unsigned long oldSize = storageUnit->GetSize(ev);
  1363.  
  1364.     storageUnit->SetOffset(ev, 0);
  1365.  
  1366.     FW_CByteArray byteArray(buffer, bufSize);
  1367.     storageUnit->SetValue(ev, byteArray);
  1368.  
  1369.     if (oldSize > bufSize)
  1370.         storageUnit->DeleteValue(ev, oldSize - bufSize);
  1371. }
  1372.  
  1373. //----------------------------------------------------------------------------------------
  1374. // LoadHandle
  1375. //----------------------------------------------------------------------------------------
  1376.  
  1377. FW_Boolean LoadHandle(
  1378.             Environment*         ev,
  1379.             ODStorageUnit*         storageUnit,
  1380.             const char*            propName,
  1381.             const char*            propType,
  1382.             FW_PlatformHandle&    handle)
  1383. {
  1384.     if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
  1385.     {
  1386.         storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
  1387.         unsigned long size = storageUnit->GetSize(ev);
  1388.         FW_CAcquireTemporarySystemHandle tempHandle(size);
  1389.         void* buffer = tempHandle.GetPointer();
  1390.         
  1391.         unsigned long sizeLoaded = 0;
  1392.         if (::LoadBuffer(ev, storageUnit, propName, propType, buffer, sizeLoaded))
  1393.         {
  1394.             handle = tempHandle.GetPlatformHandle();
  1395.             tempHandle.Orphan();
  1396.             return TRUE;
  1397.         }
  1398.     }
  1399.  
  1400.     return FALSE;
  1401. }
  1402.  
  1403. //----------------------------------------------------------------------------------------
  1404. // LoadBuffer
  1405. //----------------------------------------------------------------------------------------
  1406.  
  1407. FW_Boolean     LoadBuffer(
  1408.             Environment*         ev,
  1409.             ODStorageUnit*         storageUnit,
  1410.             const char*            propName,
  1411.             const char*            propType,
  1412.             void*                buffer,
  1413.             unsigned long&        bufSize)
  1414. {
  1415.     if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
  1416.     {
  1417.         storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
  1418.         storageUnit->SetOffset(ev, 0);
  1419.  
  1420.         bufSize = storageUnit->GetSize(ev);
  1421.     
  1422. #if FW_OPENDOC_VERSION >= FW_OPENDOC_DR3
  1423.         FW_CByteArray byteArray;
  1424.         storageUnit->GetValue(ev, bufSize, byteArray);
  1425.         byteArray.CopyBuffer(buffer, bufSize);
  1426. #else
  1427.         FW_CByteArray byteArray(buffer, bufSize);
  1428.         storageUnit->GetValue(ev, byteArray);
  1429. #endif
  1430.  
  1431.         return TRUE;
  1432.     }
  1433.     
  1434.     return FALSE;
  1435. }
  1436.  
  1437.